/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.facebook.drawee.view.SimpleDraweeView;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.Niudan;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.simon.mengkou.ui.activity.NDNiudanActivity;
import com.simon.mengkou.ui.view.StrokeTextView;
import com.simon.mengkou.utils.UtilOuer;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/1/13.
 * desc    : 扭蛋选择适配器
 */
public class NDOptionsAdapter extends AbsAdapter<Niudan> {
    private NDNiudanActivity mActivity;

    public NDOptionsAdapter(NDNiudanActivity context) {
        super(context);
        mActivity = context;
    }

    @Override
    public int getCount() {
        int count = super.getCount();
        return count%2==0 ? count : count+1;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        final Holder holder;
        if (null == convertView) {
            convertView = mInflater.inflate(R.layout.layout_options_list_item, null);
            holder = new Holder();
            convertView.setTag(holder);
            ButterKnife.bind(holder, convertView);
        } else {
            holder = (Holder) convertView.getTag();
        }

        final Niudan data = getItem(position);
        if(data == null) {
            holder.mRlTaobaoPriceRoot.setVisibility(View.INVISIBLE);
            holder.mRlImageRoot.setVisibility(View.INVISIBLE);
            holder.mRlPriceRoot.setVisibility(View.INVISIBLE);
        } else {
            holder.mRlTaobaoPriceRoot.setVisibility(View.VISIBLE);
            holder.mRlImageRoot.setVisibility(View.VISIBLE);
            holder.mRlPriceRoot.setVisibility(View.VISIBLE);

            if(position %2 == 0) {
                holder.mRlPriceRoot.setBackgroundResource(R.drawable.options_bg_moekou_price_red);
                holder.mStvPrice.setTextColor(mContext.getResources().getColor(R.color.stroke_white),
                        mContext.getResources().getColor(R.color.stroke_brown));
            } else {
                holder.mRlPriceRoot.setBackgroundResource(R.drawable.options_bg_moekou_price_yellow);
                holder.mStvPrice.setTextColor(mContext.getResources().getColor(R.color.stroke_white),
                        mContext.getResources().getColor(R.color.stroke_yellow));
            }

            if(position %4 == 0) {
                holder.mIvBorder.setBackgroundResource(R.drawable.options_bg_image_cyan_border);
            } else if(position %4 == 1) {
                holder.mIvBorder.setBackgroundResource(R.drawable.options_bg_image_blue_border);
            } else if(position %4 == 2) {
                holder.mIvBorder.setBackgroundResource(R.drawable.options_bg_image_red_border);
            } else if(position %4 == 3) {
                holder.mIvBorder.setBackgroundResource(R.drawable.options_bg_image_yellow_border);
            }

            //价格
            String price = UtilOuer.formatPrice(data.getPrice());
            holder.mStvPrice.setText(mContext.getString(R.string.options_price, price));

            //淘宝价格
            String taobaoPrice = UtilOuer.formatPrice(data.getTaobaoPrice());
            holder.mStvTaobaoPrice.setText(mContext.getString(R.string.options_taobao_price, taobaoPrice));

            //图片
            OuerApplication.mImageLoader.loadSkewXImage(holder.mSdvImage, data.getImage().getUrl());
            holder.mSdvImage.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v) {
                    mActivity.closeDoor(new NDNiudanActivity.OnDoorCloseListener() {
                        @Override
                        public void onClose() {
                            //打开扭蛋详情
                            OuerDispatcher.presentDetailFragment(mActivity, data);
                        }
                    }, 0);

                }
            });

        }

        return convertView;
    }


    /**
     * @author zhenshui.xia
     */
    class Holder {
        //扭蛋淘宝价格
        @Bind(R.id.options_id_taobao_price)
        StrokeTextView mStvTaobaoPrice;
        //扭蛋淘宝价格 root
        @Bind(R.id.options_id_taobao_price_root) RelativeLayout mRlTaobaoPriceRoot;
        //扭蛋价格
        @Bind(R.id.options_id_price)
        StrokeTextView mStvPrice;
        //扭蛋价格 root
        @Bind(R.id.options_id_price_root) RelativeLayout mRlPriceRoot;
        //扭蛋图片
        @Bind(R.id.options_id_image) SimpleDraweeView mSdvImage;
        //边框
        @Bind(R.id.options_id_border)ImageView mIvBorder;
        //边框image root
        @Bind(R.id.options_id_image_root) RelativeLayout mRlImageRoot;
    }
}
